<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px"
      class="white-normal-box">
      <el-form-item label="企业名称" prop="enterpriseName">
        <el-input v-model="queryParams.enterpriseName" placeholder="请输入企业名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="统一社会信用代码" prop="credit">
        <el-input v-model="queryParams.credit" placeholder="请输入统一社会信用代码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="营业执照图片" prop="businessLicense">
        <el-input
          v-model="queryParams.businessLicense"
          placeholder="请输入营业执照图片"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="用户名称" prop="userNames">
        <el-input v-model="queryParams.userNames" placeholder="请输入用户名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="法人姓名" prop="userName">
        <el-input v-model="queryParams.userName" placeholder="请输入法人姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="法人身份证" prop="identityCard">
        <el-input
          v-model="queryParams.identityCard"
          placeholder="请输入法人身份证"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="手机号码" prop="phoneNumber">
        <el-input v-model="queryParams.phoneNumber" placeholder="请输入手机号码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="正面照片" prop="fullFacePhoto">
        <el-input
          v-model="queryParams.fullFacePhoto"
          placeholder="请输入正面照片"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="反面照片" prop="backPhoto">
        <el-input
          v-model="queryParams.backPhoto"
          placeholder="请输入反面照片"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="地址" prop="site">
        <el-input
          v-model="queryParams.site"
          placeholder="请输入地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户id" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预留字段" prop="remok">
        <el-input
          v-model="queryParams.remok"
          placeholder="请输入预留字段"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <div class="white-normal-box">
      <el-row :gutter="10" class="mb20">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
            v-hasPermi="['system:name:add']">新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
            v-hasPermi="['system:name:edit']">修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
            v-hasPermi="['system:name:remove']">删除</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
            v-hasPermi="['system:name:export']">导出</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="nameList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="主键id" align="center" prop="id" />
        <el-table-column label="企业名称" align="center" prop="enterpriseName" />
        <el-table-column label="统一社会信用代码" align="center" prop="credit" />
        <el-table-column label="营业执照图片" prop="businessLicense" align="center" width="100px">
          <template slot-scope="scope">
            <image-preview :src="scope.row.businessLicense" :width="50" :height="50" />
          </template>
        </el-table-column>
        <el-table-column label="用户名称" align="center" prop="userNames" />
        <el-table-column label="法人姓名" align="center" prop="userName" />
        <el-table-column label="法人身份证" align="center" prop="identityCard" />
        <el-table-column label="手机号码" align="center" prop="phoneNumber" />
        <el-table-column label="正面照片" prop="fullFacePhoto" align="center" width="100">
          <template slot-scope="scope">
            <image-preview :src="scope.row.fullFacePhoto" :width="50" :height="50" />
          </template>
        </el-table-column>
        <el-table-column label="反面照片" prop="backPhoto" align="center" width="100">
          <template slot-scope="scope">
            <image-preview :src="scope.row.backPhoto" :width="50" :height="50" />
          </template>
        </el-table-column>
        <el-table-column label="地址" align="center" prop="site" />
        <!-- <el-table-column label="用户id" align="center" prop="userId" />
        <el-table-column label="预留字段" align="center" prop="remok" /> -->
        <el-table-column label="实名类型" align="center" prop="remok">
          <template slot-scope="scope">
            <span v-if="scope.row.remok == 0">个人</span>
            <span v-if="scope.row.remok == 1">对公</span>
            <span v-if="scope.row.remok == 2">法人</span>

          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
              v-hasPermi="['system:name:edit']">修改</el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
              v-hasPermi="['system:name:remove']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getList" />
    </div>

    <!-- 添加或修改实名认证对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="企业名称" prop="enterpriseName">
          <el-input v-model="form.enterpriseName" placeholder="请输入企业名称" />
        </el-form-item>
        <el-form-item label="统一社会信用代码" prop="credit">
          <el-input v-model="form.credit" placeholder="请输入统一社会信用代码" />
        </el-form-item>
        <el-form-item label="营业执照图片" prop="businessLicense">
          <el-input v-model="form.businessLicense" placeholder="请输入营业执照图片" />
        </el-form-item>
        <el-form-item label="法人姓名" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入法人姓名" />
        </el-form-item>
        <el-form-item label="法人身份证" prop="identityCard">
          <el-input v-model="form.identityCard" placeholder="请输入法人身份证" />
        </el-form-item>
        <el-form-item label="手机号码" prop="phoneNumber">
          <el-input v-model="form.phoneNumber" placeholder="请输入手机号码" />
        </el-form-item>
        <el-form-item label="正面照片" prop="fullFacePhoto">
          <el-input v-model="form.fullFacePhoto" placeholder="请输入正面照片" />
        </el-form-item>
        <el-form-item label="反面照片" prop="backPhoto">
          <el-input v-model="form.backPhoto" placeholder="请输入反面照片" />
        </el-form-item>
        <el-form-item label="地址" prop="site">
          <el-input v-model="form.site" placeholder="请输入地址" />
        </el-form-item>
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="预留字段" prop="remok">
          <el-input v-model="form.remok" placeholder="请输入预留字段" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listName, getName, delName, addName, updateName } from "@/api/system/name";

export default {
  name: "Name",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 实名认证表格数据
      nameList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        enterpriseName: null,
        credit: null,
        businessLicense: null,
        userName: null,
        userNames: null,
        identityCard: null,
        phoneNumber: null,
        fullFacePhoto: null,
        backPhoto: null,
        site: null,
        userId: null,
        remok: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询实名认证列表 */
    getList() {
      this.loading = true;
      listName(this.queryParams).then(response => {
        this.nameList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        enterpriseName: null,
        credit: null,
        businessLicense: null,
        userName: null,
        identityCard: null,
        phoneNumber: null,
        fullFacePhoto: null,
        backPhoto: null,
        site: null,
        userId: null,
        remok: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加实名认证";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getName(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改实名认证";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateName(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addName(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除实名认证编号为"' + ids + '"的数据项？').then(function () {
        return delName(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/name/export', {
        ...this.queryParams
      }, `name_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
